package com.exercises;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Exercise43 {
    public static void main(String[] args) {
        String aa = "123";
        System.out.println(new Exercise43().multiply("123", "456"));
    }


    public String multiply(String num1, String num2) {
        if ("0".equals(num1) || "0".equals(num2)) {
            return "0";
        }
        Integer[] arr = new Integer[num1.length() + num2.length()];
        Arrays.fill(arr, 0);
        for (int i = 0; i < num1.length(); i++) {
            for (int j = 0; j < num2.length(); j++) {
                int a = Integer.valueOf(String.valueOf(num1.charAt(num1.length() - i - 1)));
                int b = Integer.valueOf(String.valueOf(num2.charAt(num2.length() - j - 1)));
                int result = a * b;
                Integer exist = arr[i + j];
                arr[i + j] = result + exist;
            }
        }

        for (int i = 0; i < arr.length; i++) {
            Integer exist = arr[i];
            if (exist > 9) {
                arr[i] = exist % 10;
                arr[i + 1] = exist / 10 + arr[i + 1];
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (Integer i : arr) {
            stringBuilder.insert(0, i);
        }
        return stringBuilder.toString().replaceFirst("0*", "");
    }


}
